﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mighty.Common;
using System.Collections;

//THANKS TO 진용선배

namespace Mighty.Brain
{
    public class _2NE3_5Brain : BasicBrain
    {
       
        // 무늬별 나의 카드 뭉치들 
        public int[] cloverCards = new int[15];
        public int[] diamondCards = new int[15];
        public int[] spadeCards = new int[15];
        public int[] heartCards = new int[15];
        bool haveJoker = false; // 조커 유무
        bool haveJokercall = false; // 조커콜 유뮤
        
        /* 게임 시작 전 초기화 */

        public override void GameStartEvent()
        {
            haveJoker = false;
            haveJokercall = false;
        }
        

        /* 배팅 전에 내 카드 정리, 모양별로 배팅가중치로 변환 */

        public override void BettingReadyEvent()
        {
            for (int i = 0; i < 10; i++)
            {
                HandedCard c = gameInfo.myCards[i];
                int s = (int)c.shape;

                switch (s)
                {
                    case 0:
                        cloverCards[c.number] = 1;
                        if (c.number == 10 || c.number == 11)
                        {
                            cloverCards[c.number] = 2;
                        }
                        else if (c.number == 12 || c.number == 13)
                        {
                            cloverCards[c.number] = 3;
                        }
                        else if (c.number == 14)
                        {
                            cloverCards[c.number] = 4;
                        }
                        break;
                    case 1:
                        diamondCards[c.number] = 1;
                        if (c.number == 10 || c.number == 11)
                        {
                            diamondCards[c.number] = 2;
                        }
                        else if (c.number == 12 || c.number == 13)
                        {
                            diamondCards[c.number] = 3;
                        }
                        else if (c.number == 14)
                        {
                            diamondCards[c.number] = 4;
                        }
                        break;
                    case 2:
                        spadeCards[c.number] = 1;
                        if (c.number == 10 || c.number == 11)
                        {
                            spadeCards[c.number] = 2;
                        }
                        else if (c.number == 12 || c.number == 13)
                        {
                            spadeCards[c.number] = 3;
                        }
                        else if (c.number == 14)
                        {
                            spadeCards[c.number] = 4;
                        }
                        break;
                    case 3:
                        heartCards[c.number] = 1;
                        if (c.number == 10 || c.number == 11)
                        {
                            heartCards[c.number] = 2;
                        }
                        else if (c.number == 12 || c.number == 13)
                        {
                            heartCards[c.number] = 3;
                        }
                        else if (c.number == 14)
                        {
                            heartCards[c.number] = 4;
                        }
                        break;
                    case 4:
                        haveJoker = true;
                        break;
                }

            }
        }

        
        public override Betting BettingTicEvent()
        {
            Betting betting = new Betting();
            int[] total_score= new int[4];                  ///// 모양별 가중치
            int i, j, max;
            int My_limit;


            /*         모양별 배팅가중치 합산            */

            for (j = 0; j < 15; j++ )
                total_score[0] += cloverCards[j];
            for (j = 0; j < 15; j++)
                total_score[1] += diamondCards[j];
            for (j = 0; j < 15; j++)
                total_score[2] += spadeCards[j];
            for (j = 0; j < 15; j++)
                total_score[3] += heartCards[j];


            if (haveJoker)
            {
                for (i = 0; i < 4; i++)
                {

                    total_score[i] += 4;
                }
            }
            
            /*        내가 배팅하려는 모양 고르기       */
            max = 0;
            for (i = 0; i < 4; i++)
            {
                if (total_score[i] > max)
                    max = total_score[i];
            }
            j = 0;
            for (i = 0; i < 4; i++)
            {
                if (max == total_score[i])
                    j = i;
            }

            if (j == 2)
            {
                if (diamondCards[14]!=0)
                    total_score[j] += 4;
            }
            else
            {
                if (spadeCards[14]!=0)
                    total_score[j] += 4;
            }

            My_limit = 0;
            if (total_score[j] == 9)
            {
                My_limit = 14;
            }
            else if (9 < total_score[j] && total_score[j] <= 11)
            {
                My_limit = 15;
            }
            else if (11 < total_score[j]&&total_score[j]<=15)
            {
                My_limit = 16;
            }else if (15 < total_score[j])
            {
                My_limit = 17;
            }

            /* 기루다 정하기 */
            // 누군가가 배팅을 했을 경우
            if (gameInfo.betting.limit > 0)
            {
                Betting previousBetting = (Betting)gameInfo.bettingHistory[gameInfo.bettingHistory.Count - 1];


                // 내 카드의 배팅 한도보다 현재 배팅 한도가 작으면 배팅한다
                if (previousBetting.limit < My_limit)
                {
                    betting.isPass = false;
                    betting.limit = previousBetting.limit + 1;

                    switch (j)
                    {
                        case 0:
                            betting.giruda = Giruda.Clover;
                            break;
                        case 1:
                            betting.giruda = Giruda.Diamond;
                            break;
                        case 2:
                            betting.giruda = Giruda.Spade;
                            break;
                        case 3:
                            betting.giruda = Giruda.Heart;
                            break;
                    }
                }
            }
            // 아직 아무도 배팅한 사람이 없을 경우
            else if (My_limit != 0)
            {
                    betting.isPass = false;
                    betting.limit = My_limit;

                    switch (j)
                    {
                        case 0:
                            betting.giruda = Giruda.Clover;
                            break;
                        case 1:
                            betting.giruda = Giruda.Diamond;
                            break;
                        case 2:
                            betting.giruda = Giruda.Spade;
                            break;
                        case 3:
                            betting.giruda = Giruda.Heart;
                            break;
                    }
              }else{
                    betting.isPass=true;
              }


            return betting;
        }

        public override HandedCard RoundTicEvent()
        {
            ArrayList possibleCards = new ArrayList();
            foreach (HandedCard card in gameInfo.myCards)
            {
                if (card.isPossible) possibleCards.Add(card);
            }

            // 10라운드
            if (gameInfo.roundIdx == 9)
            {
                return (HandedCard)possibleCards[0];
            }
            
            if (gameInfo.myInfo.position == gameInfo.attackPlayer.position)
            {
                //주공일 때
                return AttackRoundTIcEvent(possibleCards);
            }
            else if (gameInfo.myInfo.position == gameInfo.friend.position)
            {
                // 프렌드일떄
            }
            else
            {
                // 수비일 때
                return DefenseRoundTIcEvent(possibleCards);
            }

            Random r = new Random();
            int k = r.Next(possibleCards.Count);

            return (HandedCard)possibleCards[k];
        }

        public HandedCard DefenseRoundTIcEvent(ArrayList possibleCards)
        {
            Random r = new Random();
            int k;

            if (gameInfo.roundCardIdx == 0)
            {
                //선

            }
            else
            {
                //후

                int attackFlag = 0;
                // 0 : 둘다안냄
                // 1 : 주공만
                // 2 : 프렌드만
                // 3 : 둘다


                // 플래그 체크

                RoundCard[] nowRound = gameInfo.roundHistory[gameInfo.roundIdx].cardStack;

                for (int i = 0; i < gameInfo.roundCardIdx; i++)
                {
                    RoundCard nowTurnCard = nowRound[i];

                    if (nowTurnCard.owner == gameInfo.attackPlayer.position)
                    {
                        attackFlag += 1;
                    }
                    if (nowTurnCard.owner == gameInfo.friend.position)
                    {
                        attackFlag += 2;
                    }
                }

                switch (attackFlag)
                {
                    case 0:
                        break;

                    case 1:
                    case 2:

                        break;

                    case 3:
                        RoundCard winCard = null;
                        int winPlayer = -1;
                        int grimCount = 0;

                        for (int i = 0; i < gameInfo.roundCardIdx; i++)
                        {
                            RoundCard nowCard = nowRound[i];

                            if (nowCard.number > 9)
                            {
                                grimCount++;
                            }

                            if (winCard != null)
                            {
                                if (nowCard.shape == gameInfo.mighty.shape &&
                                    nowCard.number == gameInfo.mighty.number)
                                {
                                    winCard = nowCard;
                                    winPlayer = nowCard.owner;
                                }
                                if (nowCard.shape == Shape.Joker)
                                {
                                    winCard = nowCard;
                                    winPlayer = nowCard.owner;
                                }
                                if (winCard.shape == (Shape)gameInfo.betting.giruda)
                                {
                                    if (nowCard.shape == (Shape)gameInfo.betting.giruda)
                                    {
                                        if (nowCard.number > winCard.number)
                                        {
                                            winCard = nowCard;
                                            winPlayer = nowCard.owner;
                                        }
                                    }
                                }
                                else
                                {
                                    if (nowCard.shape == (Shape)gameInfo.betting.giruda)
                                    {
                                        winCard = nowCard;
                                        winPlayer = nowCard.owner;
                                    }
                                    else
                                    {
                                        if (nowCard.number > winCard.number)
                                        {
                                            winCard = nowCard;
                                            winPlayer = nowCard.owner;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                winCard = nowCard;
                                winPlayer = nowCard.owner;
                            }
                        }

                        if (winPlayer == gameInfo.attackPlayer.position ||
                            winPlayer == gameInfo.friend.position)
                        {
                            // 주공 프렌드가 둘중 하나가 이깁니다.

                            ArrayList myWinCardList = new ArrayList();

                            if (winCard.shape != gameInfo.mighty.shape ||
                                winCard.number != gameInfo.mighty.number)
                            {
                                for (int i = 0; i < possibleCards.Count; i++)
                                {
                                    HandedCard nowCard = (HandedCard)possibleCards[i];

                                    if (winCard.shape == Shape.Joker)
                                    {
                                        if (nowCard.shape == gameInfo.mighty.shape &&
                                            nowCard.number == gameInfo.mighty.number)
                                        {
                                            myWinCardList.Add(nowCard);
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        if (winCard.shape == (Shape)gameInfo.betting.giruda)
                                        {
                                            if (nowCard.shape == (Shape)gameInfo.betting.giruda)
                                            {
                                                if (nowCard.number > winCard.number)
                                                {
                                                    myWinCardList.Add(nowCard);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (nowCard.shape == (Shape)gameInfo.betting.giruda)
                                            {
                                                myWinCardList.Add(nowCard);
                                            }
                                            else
                                            {
                                                if (nowCard.number > winCard.number)
                                                {
                                                    myWinCardList.Add(nowCard);
                                                }
                                            }
                                        }
                                    }
                                }

                                if (grimCount > 1 && myWinCardList.Count > 0)
                                {
                                    // 내가 그 둘을 이길 수 있을때
                                    k = r.Next(myWinCardList.Count);
                                    return (HandedCard)myWinCardList[k];
                                }

                                // 질때 낮은거 냅시다

                                HandedCard minCard = null;

                                for (int i = 0; i < possibleCards.Count; i++)
                                {
                                    HandedCard nowCard = (HandedCard)possibleCards[i];

                                    if (nowCard.shape != (Shape)gameInfo.betting.giruda)
                                    {
                                        if (minCard != null)
                                        {
                                            if (minCard.number > nowCard.number)
                                            {
                                                minCard = nowCard;
                                            }
                                        }
                                        else
                                        {
                                            minCard = nowCard;
                                        }
                                    }
                                }

                                if (minCard != null)
                                {
                                    return minCard;
                                }
                            }
                        }
                        else
                        {
                            // 주공과 프렌드가 이미 냈는데 둘다 질 카드인 경우
                            
                            HandedCard grimCard = null;

                            for (int i = 0; i < possibleCards.Count; i++)
                            {
                                HandedCard nowCard = (HandedCard)possibleCards[i];

                                if (nowCard.number > 9)
                                {
                                    if (nowCard.shape == gameInfo.mighty.shape &&
                                        nowCard.number == gameInfo.mighty.number)
                                    {
                                        continue;
                                    }
                                    if (nowCard.shape == Shape.Joker)
                                    {
                                        continue;
                                    }
                                    if (grimCard != null)
                                    {
                                        if (nowCard.number < grimCard.number)
                                        {
                                            grimCard = nowCard;
                                        }
                                    }
                                    else
                                    {
                                        grimCard = nowCard;
                                    }
                                }
                            }

                            // 그림 내기
                            if (grimCard != null)
                            {
                                return grimCard;
                            }
                        }

                        break;
                }
            }




            k = r.Next(possibleCards.Count);

            return (HandedCard)possibleCards[k];
        }

        public HandedCard AttackRoundTIcEvent(ArrayList possibleCards)
        {

            Random r = new Random();

            // 1라운드
            if (gameInfo.roundIdx == 0)
            {
                HandedCard minCard = (HandedCard)possibleCards[0];

                for (int i = 1; i < possibleCards.Count; i++)
                {
                    HandedCard nowCard = (HandedCard)possibleCards[i];

                    if (minCard.shape == Shape.Joker)
                    {
                        minCard = nowCard;
                    }

                    if (minCard.number > nowCard.number)
                    {
                        if (nowCard.shape != Shape.Joker)
                        {
                            minCard = nowCard;
                        }
                    }
                }
                return minCard;
            }

            // 2-9라운드 선공
            if (gameInfo.roundCardIdx == 0)
            {
                ArrayList aceCards = new ArrayList();
                HandedCard minGiruda = null;
                HandedCard maxNoGiruda = null;
                HandedCard jokerCard = null;

                for (int i = 0; i < possibleCards.Count; i++)
                {
                    HandedCard nowCard = (HandedCard)possibleCards[i];

                    if (nowCard.shape != (Shape)gameInfo.betting.giruda)
                    {
                        if (nowCard.number == 14)
                        {
                            aceCards.Add(nowCard);
                        }

                        if (maxNoGiruda == null)
                        {
                            maxNoGiruda = nowCard;
                        }
                        else
                        {
                            if (maxNoGiruda.number < nowCard.number)
                            {
                                maxNoGiruda = nowCard;
                            }
                        }
                    }
                    else
                    {
                        if (minGiruda == null)
                        {
                            minGiruda = nowCard;
                        }
                        else
                        {
                            if (minGiruda.number > nowCard.number)
                            {
                                minGiruda = nowCard;
                            }
                            if (nowCard.shape == Shape.Joker)
                            {
                                jokerCard = nowCard;
                                jokerCard.cmdJokerTarget = (Shape)gameInfo.betting.giruda;
                            }
                        }
                    }

                }
                if (aceCards.Count > 0)
                {
                    int k = r.Next(aceCards.Count);
                    return (HandedCard)aceCards[k];
                }
                if (minGiruda != null)
                {
                    if (minGiruda.number > 9)
                    {
                        return jokerCard;
                    }
                    return minGiruda;
                }
                if (maxNoGiruda != null)
                {
                    return maxNoGiruda;
                }
            }
            else
            {
                // 2-9라운드 후공

                // 그림 체크
                int grimCount = 0;
                RoundCard[] nowRound = gameInfo.roundHistory[gameInfo.roundIdx].cardStack;
                RoundCard winCard = null;
                int winPlayer = -1;

                for (int i = 0; i < gameInfo.roundCardIdx; i++)
                {
                    if (nowRound[i].number >= 10)
                    {
                        grimCount++;
                    }
                    if (winCard != null)
                    {
                        if (nowRound[i].shape != (Shape)gameInfo.betting.giruda)
                        {
                            if (nowRound[i].shape == winCard.shape)
                            {
                                if (nowRound[i].number > winCard.number)
                                {
                                    winCard = nowRound[i];
                                    winPlayer = nowRound[i].owner;
                                }
                            }
                        }
                        else
                        {
                            if (nowRound[i].shape == winCard.shape)
                            {
                                if (nowRound[i].number > winCard.number)
                                {
                                    winCard = nowRound[i];
                                    winPlayer = nowRound[i].owner;
                                }
                            }
                            else
                            {
                                winCard = nowRound[i];
                                winPlayer = nowRound[i].owner;
                            }
                        }
                    }
                    else
                    {
                        winCard = nowRound[i];
                        winPlayer = nowRound[i].owner;
                    }
                }

                //분기!
                if (grimCount > 1)
                {
                    // 조커있음 조커
                    HandedCard JokerCard = null;

                    for (int i = 0; i < possibleCards.Count; i++)
                    {
                        HandedCard nowCard = (HandedCard)possibleCards[i];

                        if (nowCard.shape == Shape.Joker)
                        {
                            JokerCard = nowCard;
                            break;
                        }
                    }

                    if (JokerCard != null)
                    {
                        return JokerCard;
                    }

                    // 이기는 쪽으로

                    HandedCard myWinCard = null;

                    for (int i = 0; i < possibleCards.Count; i++)
                    {
                        HandedCard nowCard = (HandedCard)possibleCards[i];

                        if (myWinCard != null)
                        {
                            if (gameInfo.mighty.number == nowCard.number &&
                                gameInfo.mighty.shape == nowCard.shape)
                            {
                                myWinCard = nowCard;
                                break;
                            }
                            if (nowCard.shape == Shape.Joker)
                            {
                                myWinCard = nowCard;
                                break;
                            }
                            if (nowCard.shape != (Shape)gameInfo.betting.giruda)
                            {
                                if (nowCard.shape == myWinCard.shape)
                                {
                                    if (nowCard.number > myWinCard.number)
                                    {
                                        myWinCard = nowCard;
                                    }
                                }
                            }
                            else
                            {
                                if (nowCard.shape == myWinCard.shape)
                                {
                                    if (nowCard.number > myWinCard.number)
                                    {
                                        myWinCard = nowCard;
                                    }
                                }
                                else
                                {
                                    myWinCard = nowCard;
                                }
                            }
                        }
                        else
                        {
                            myWinCard = nowCard;
                        }
                    }

                    //이기는 카드가 있는지 체크

                    if (myWinCard != null)
                    {
                        return myWinCard;
                    }

                    if (winPlayer == gameInfo.friend.position)
                    {
                        HandedCard grimCard = null;

                        for (int i = 0; i < possibleCards.Count; i++)
                        {
                            HandedCard nowCard = (HandedCard)possibleCards[i];

                            if (nowCard.number >= 10)
                            {
                                if (grimCard != null)
                                {
                                    if (nowCard.shape != (Shape)gameInfo.betting.giruda)
                                    {
                                        if (grimCard.shape == (Shape)gameInfo.betting.giruda)
                                        {
                                            grimCard = nowCard;
                                        }
                                    }

                                    if (grimCard.number > nowCard.number)
                                    {
                                        grimCard = nowCard;
                                    }
                                }
                                else
                                {
                                    grimCard = nowCard;
                                }
                            }
                        }

                        if (grimCard != null)
                        {
                            return grimCard;
                        }
                    }
                    else
                    {
                        // 못이길 경우 낮은 카드로

                        HandedCard minCard = null;

                        for (int i = 0; i < possibleCards.Count; i++)
                        {
                            HandedCard nowCard = (HandedCard)possibleCards[i];

                            if (minCard != null)
                            {
                                if (nowCard.shape != Shape.Joker)
                                {
                                    if (nowCard.number < minCard.number)
                                    {
                                        minCard = nowCard;
                                    }
                                }
                            }
                            else
                            {
                                if (nowCard.shape != Shape.Joker)
                                {
                                    minCard = nowCard;
                                }
                            }
                        }

                        if (minCard != null)
                        {
                            return minCard;
                        }
                    }
                }
                else
                {
                    // 그림이 2개 이상이 아닐때

                    int girudaCount = 0;
                    int girudaNumberMax = 0;

                    for (int i = 0; i < gameInfo.roundCardIdx; i++)
                    {
                        RoundCard nowCard = nowRound[i];

                        if (nowCard.shape == (Shape)gameInfo.betting.giruda)
                        {
                            if (nowCard.number > girudaNumberMax)
                            {
                                girudaNumberMax = nowCard.number;
                            }
                            girudaCount++;
                        }
                    }

                    if (girudaCount > 0)
                    {
                        ArrayList myWinCardList = new ArrayList();

                        for (int i = 0; i < possibleCards.Count; i++)
                        {
                            HandedCard nowCard = (HandedCard)possibleCards[i];

                            if (nowCard.shape == (Shape)gameInfo.betting.giruda)
                            {
                                if (nowCard.number > girudaNumberMax)
                                {
                                    myWinCardList.Add(nowCard);
                                }
                            }
                        }

                        if (myWinCardList.Count > 0)
                        {
                            int k = r.Next(myWinCardList.Count);

                            return (HandedCard)myWinCardList[k];
                        }
                        else
                        {
                            int k = r.Next(possibleCards.Count);

                            return (HandedCard)possibleCards[k];
                        }
                    }
                    else
                    {
                        int k = r.Next(possibleCards.Count);

                        return (HandedCard)possibleCards[k];
                    }
                }
            }
            

            return (HandedCard)possibleCards[0];
        }


        public override HandedCard[] ChoiceCardEvent()
        {
            Betting betting  = new Betting();
            int count=0;

            for (int i = 10; i < 13; i++)
            {
                if ((int)gameInfo.myCards[i].shape == (int)betting.giruda)
                {
                    gameInfo.myCards[i].cmdRemove = false;
                    count++;
                }
            }
            for (int i = 0; i < 10; i++)
            {
                if (count == 0) break;
                if ((int)gameInfo.myCards[i].shape != (int)betting.giruda && (int)gameInfo.myCards[i].number < 10)
                {
                    gameInfo.myCards[i].cmdRemove = true;
                    count--;
                }
            }
            

            return gameInfo.myCards;
        }

    }

    public class playerInfo
    {
        int totlaOutGiruda;
        int[] playerOutGiruda = new int[5];
        
        
    }

    public class cardInfo
    {
        
    }
}
